import copy
import random

from flask import current_app


def IsStandbyException(response):
    try:
        if response.json()["RemoteException"]["exception"] == "StandbyException":
            return True
        else:
            return False
    except:
        print("not StandbyException err")
        return False


def set_other_hdfs():
    uri = current_app.config.get("WEB_HDFS")
    lists = current_app.config.get("WEB_HDFS_LISTS")
    lists2 = copy.deepcopy(lists)
    lists2.remove(uri)
    webhdfs = random.choice(lists2)
    if webhdfs:
        print(webhdfs)
        current_app.config["WEB_HDFS"] = webhdfs
        return True
    return False


def change_hdfs(response):
    if IsStandbyException(response):
        if set_other_hdfs():
            return True
